package com.dragon.utilslibrary.network.http;


import com.dragon.utilslibrary.network.HttpThreadPoolUtil;
import com.dragon.utilslibrary.network.http.syn.FileUploaderSyn;
import com.dragon.utilslibrary.network.http.syn.HttpSendSyn;

public class HttpUtils {
	private FileUploader fileUploader;
	private HttpSend httpSend;
	private FileUploaderSyn fileUploaderSyn;
	private HttpSendSyn httpSendSyn;
	/**
	 * 异步请求
	 * @param request
	 * @param listioner
	 */
	public void send(final HttpRequest request,final HttpListioner<HttpRespose> listioner){
		if(request.getFileParems() != null && !request.getFileParems().isEmpty()){//文件上传
			fileUploader = new FileUploader();
			HttpThreadPoolUtil.getInstands().getPool().execute(new Runnable() {
				@Override
				public void run() {
					// TODO Auto-generated method stub
					fileUploader.upload(request, listioner);
				}
			});
		}else{//普通http请求
			httpSend = new HttpSend();
			HttpThreadPoolUtil.getInstands().getPool().execute(new Runnable() {
				@Override
				public void run() {
					// TODO Auto-generated method stub
					httpSend.send(request, listioner);
				}
			});
		}
	}
	/**
	 * 同步请求
	 * @param request
	 * @param listioner (返回上传或下载的进度，子线程)
	 * @return
	 */
	public HttpRespose sendSyn(final HttpRequest request,final HttpListioner<HttpRespose> listioner){
		if(request.getFileParems() != null && !request.getFileParems().isEmpty()){//文件上传
			fileUploaderSyn = new FileUploaderSyn();
			return fileUploaderSyn.upload(request, listioner);
		}else{//普通http请求
			httpSendSyn = new HttpSendSyn();
			return httpSendSyn.send(request, listioner);
		}
	}
	/**
	 * 取消
	 */
	public void cancelled(){
		if(httpSend != null){
			httpSend.cancelled();
		}
		if(fileUploader != null){
			fileUploader.cancelled();
		}
		if(httpSendSyn != null){
			httpSendSyn.cancelled();
		}
		if(fileUploaderSyn != null){
			fileUploaderSyn.cancelled();
		}
	}
}
